package com.umeng.soexample.mvp.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.umeng.soexample.R;
import com.umeng.soexample.eventbus_bean.BaseEventBusEntity;
import com.umeng.soexample.eventbus_bean.OpenEventBus;

import org.greenrobot.eventbus.EventBus;

/**
 * 逛得总视图
 * A simple {@link Fragment} subclass.
 */
public class StrollFragment extends Fragment implements View.OnClickListener {

    private ImageView iv_stroll_menu;
    private ImageView iv_stroll_see;
    private ImageView iv_stroll_community;
    private ImageView iv_stroll_show;
    private RadioGroup rg_stroll;
    private ImageView iv_stroll_top_right;

    private FragmentManager manager;
    private SeeFragment seeFragment;
    private CommunityFragment communityFragment;
    private ShowFragment showFragment;

    private int top_right_typr = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stroll, container, false);
        initView(view);
        iniListener();
        return view;
    }

    /**
     * 初始化监听
     */
    private void iniListener() {
        iv_stroll_menu.setOnClickListener(this);
        iv_stroll_top_right.setOnClickListener(this);

        rg_stroll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_stroll_see:
                        iv_stroll_see.setVisibility(View.VISIBLE);
                        iv_stroll_community.setVisibility(View.INVISIBLE);
                        iv_stroll_show.setVisibility(View.INVISIBLE);
                        showFrag(seeFragment);
                        iv_stroll_top_right.setImageResource(R.drawable.ic_heart_fill);
                        top_right_typr = 1;
                        break;
                    case R.id.rb_stroll_community:
                        iv_stroll_see.setVisibility(View.INVISIBLE);
                        iv_stroll_community.setVisibility(View.VISIBLE);
                        iv_stroll_show.setVisibility(View.INVISIBLE);
                        showFrag(communityFragment);
                        iv_stroll_top_right.setImageResource(R.drawable.ic_top_show);
                        top_right_typr = 2;
                        break;
                    case R.id.rb_stroll_show:
                        iv_stroll_see.setVisibility(View.INVISIBLE);
                        iv_stroll_community.setVisibility(View.INVISIBLE);
                        iv_stroll_show.setVisibility(View.VISIBLE);
                        showFrag(showFragment);
                        iv_stroll_top_right.setImageResource(R.drawable.community_write);
                        top_right_typr = 3;
                        break;
                }
            }
        });

        rg_stroll.check(R.id.rb_stroll_see);

    }

    /**
     * 初始化视图
     *
     * @param view
     */
    private void initView(View view) {
        manager = getActivity().getSupportFragmentManager();
        seeFragment = new SeeFragment();
        communityFragment = new CommunityFragment();
        showFragment = new ShowFragment();
        addAll();
        iv_stroll_menu = (ImageView) view.findViewById(R.id.iv_stroll_menu);
        iv_stroll_see = (ImageView) view.findViewById(R.id.iv_stroll_see);
        iv_stroll_community = (ImageView) view.findViewById(R.id.iv_stroll_community);
        iv_stroll_show = (ImageView) view.findViewById(R.id.iv_stroll_show);
        rg_stroll = (RadioGroup) view.findViewById(R.id.rg_stroll);
        iv_stroll_top_right = (ImageView) view.findViewById(R.id.iv_stroll_top_right);
    }


    /**
     * 显示指定的fragment
     */
    private void showFrag(Fragment fragment) {
        hideAll();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideAll() {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(seeFragment);
        transaction.hide(communityFragment);
        transaction.hide(showFragment);
        transaction.commit();
    }

    /**
     * 添加所有Fragment
     */
    private void addAll() {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_stroll, seeFragment);
        transaction.add(R.id.fl_stroll, communityFragment);
        transaction.add(R.id.fl_stroll, showFragment);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_stroll_menu:
                EventBus.getDefault().post(new OpenEventBus(BaseEventBusEntity.OPENMENU));
                break;
            case R.id.iv_stroll_top_right:
                Toast.makeText(getActivity(), "逛 -> 啥也没干", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
